﻿#include "param.h"
int          Param::lower_power;        //
int          Param::fail_charge_time;   //充电失败检测时间 单位s
int          Param::fail_charge_soc;    //充电失败检测电量 单位 %
int          Param::reset_charge_time;  //充电复位启动时间 单位s
int          Param::reset_charge_soc;   //充电复位检测电量 单位 %
int          Param::transmit_af_timer;
int          Param::transmit_ef_timer;
int          Param::transmit_error_timer;
int          Param::transmit_over_timer;
int          Param::enter_docking_safe_signal;
int          Param::use_dir_light;
QList< int > Param::manual_mode_obs_switch;  //手动模式遥控需要的避障，0：关闭，1：打开（前、后、左、右）
QList< int > Param::manual_mode_obs_are;     //手动模式遥控前后避障需要切换的大小(（前、后)
QList< int > Param::check_goods_number;      //上下层花篮检测数量，根据物料光感数目定，如果填0则是不使用该层
QList< int > Param::front_and_back_collision;

QString Param::machine_info_search_url;
int     Param::close_obs_left;
int     Param::close_obs_right;
int     Param::arrive_signal_open_close;
int     Param::roller_shutter_open_close;
/*串口*/
QString Param::IoControl_port_name;
int     Param::IoControl_baud_rate;
/*IO*/
QList< Param::ioParam > Param::input_io_hash_table;  //输入IO hash表
QList< Param::ioParam > Param::out_io_hash_table;    //输出IO hash表

//注意：静态变量在类定义中只是向编译器声明，并未实际分配内存，需要在类外进行定义分配内存。

/**
 * @brief Param::readParam 读取ini文件中的参数，把其更新到Param类中的静态成员变量中
 */
void Param::readParam() {
    /*串口*/
    QSettings com_setting( "./ini/ComParam.ini", QSettings::IniFormat );
    com_setting.setIniCodec( QTextCodec::codecForName( "UTF-8" ) );
    com_setting.beginGroup( "COM" );
    for ( const auto& data : com_setting.childKeys() ) {
        QString com_port_name, com_baud_rate;
        if ( com_setting.value( data ).toStringList().size() >= 2 ) {
            com_port_name = com_setting.value( data ).toStringList().at( 0 );
            com_baud_rate = com_setting.value( data ).toStringList().at( 1 );
        }
        if ( data == QString( "IOCONTROL" ) ) {
            IoControl_port_name = com_port_name.trimmed();
            IoControl_baud_rate = com_baud_rate.trimmed().toInt();
        }
    }

    //***************************[IO_PARAM]/***************************/
    if ( input_io_hash_table.size() == 0 && out_io_hash_table.size() == 0 ) {
        QSettings io_setting( "./ini/iodefineNew.ini", QSettings::IniFormat );  // UPDATE 2023.03.10 调试新的io配置表
        io_setting.setIniCodec( QTextCodec::codecForName( "UTF-8" ) );
        QStringList groupList = io_setting.childGroups();
        input_io_hash_table.clear();
        out_io_hash_table.clear();
        foreach ( QString group, groupList ) {
            // input
            if ( group == QString( "INPUT_IO_PARAM" ) ) {
                io_setting.beginGroup( group );
                QStringList             keyList = io_setting.allKeys();
                QList< Param::ioParam > demo;  //临时存储
                foreach ( QString key, keyList ) {
                    QStringList strlist = io_setting.value( key ).toStringList();
                    if ( strlist.isEmpty() || strlist.size() < 2 ) continue;  //内容空或者长度不对，都不添加
                    ioParam io_param;
                    io_param.key         = key;
                    io_param.id          = strlist.at( 0 ).toInt();
                    io_param.str_chinese = strlist.at( 1 );

                    if ( strlist.size() > 2 )
                        io_param.str_english = strlist.at( 2 );
                    else
                        io_param.str_english = "none";
                    // qDebug() << "io_param:" << io_param.key << io_param.id << io_param.str_chinese << io_param.str_english;
                    demo.append( io_param );
                }
                //重新排序
                for ( int i = 0; i < demo.size(); i++ ) {
                    foreach ( ioParam var, demo ) {
                        if ( i == var.id ) input_io_hash_table.append( var );
                    }
                }
                io_setting.endGroup();
            }
            // output
            if ( group == QString( "OUT_IO_PARAM" ) ) {
                io_setting.beginGroup( group );
                QStringList             keyList = io_setting.allKeys();
                QList< Param::ioParam > demo;  //临时存储
                foreach ( QString key, keyList ) {
                    QStringList strlist = io_setting.value( key ).toStringList();
                    if ( strlist.isEmpty() || strlist.size() < 2 ) continue;  //内容空或者长度不对，都不添加
                    ioParam io_param;
                    io_param.key         = key;
                    io_param.id          = strlist.at( 0 ).toInt();
                    io_param.str_chinese = strlist.at( 1 );

                    if ( strlist.size() > 2 )
                        io_param.str_english = strlist.at( 2 );
                    else
                        io_param.str_english = "none";
                    demo.append( io_param );
                }
                //重新排序
                for ( int i = 0; i < demo.size(); i++ ) {
                    foreach ( ioParam var, demo ) {
                        if ( i == var.id ) out_io_hash_table.append( var );
                    }
                }
                io_setting.endGroup();
            }
        }
    }
    //***************************[IO_PARAM]/***************************/
}

void Param::printParam() {
    /**********************************************************************************/
    qDebug() << "routeLog:----------------------------Read Param----------------------------";
    qDebug() << "routeLog:----------------------------特殊参数-------------------------";
}

Param::Param( QObject* parent ) : QObject( parent ) {}

Param::~Param() {}
